package com.work.straighttalk.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMVoiceMessageBody;
import com.work.straighttalk.R;
import com.work.straighttalk.app.StraightTalk;
import com.work.straighttalk.bean.StraightTalkMessage;

import butterknife.Bind;

/**
 * Created by dllo on 16/3/8.
 */
public class SendVoiceHolder extends BaseViewHolder<StraightTalkMessage> {
    @Bind(R.id.tv_length)
    TextView tvLength;
    private OnVoicePlayerListener listener;
    @Bind(R.id.sd_avater)
    SimpleDraweeView sdAvater;
    @Bind(R.id.btn_start)
    Button btnStart;
    @Bind(R.id.seek_bar)
    SeekBar seekBar;
    @Bind(R.id.btn_stop)
    Button btnStop;
    private StraightTalkMessage talkMessage;

    public SendVoiceHolder(Context context, ViewGroup root, OnVoicePlayerListener listener) {
        super(context, root, R.layout.item_voice_send);
        this.listener = listener;
    }

    @Override
    public void bindData(final StraightTalkMessage message) {
        this.talkMessage = message;
        EMMessage emMessage = talkMessage.getEmMessage();
        final EMVoiceMessageBody body = (EMVoiceMessageBody) emMessage.getBody();
        int length = body.getLength();
        seekBar.setMax(length * 1000);
        seekBar.setProgress(talkMessage.getProgress());
        tvLength.setText(length + "");
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    if (null != listener) {
                        talkMessage.setProgress(progress);
                        listener.seekTo(progress);
                    }
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Log.d("key", "inHOlder start");
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.voicePlay(message);
                    btnStop.setVisibility(View.VISIBLE);
                    btnStart.setVisibility(View.GONE);
                }
            }
        });
        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.voicePause();
                    Toast.makeText(StraightTalk.applicationContext, "message" + message.getProgress() + "seekbar" + seekBar.getProgress(), Toast.LENGTH_SHORT).show();
                    btnStart.setVisibility(View.VISIBLE);
                    btnStop.setVisibility(View.GONE);
                }
            }
        });
    }

    /**
     * 用于改变seekbar 进度
     *
     * @param progress
     */
    public void setSeekBarProgress(int progress) {
        seekBar.setProgress(progress);
        talkMessage.setProgress(progress);
        if (progress == 0) {
            btnStop.setVisibility(View.INVISIBLE);
            btnStart.setVisibility(View.VISIBLE);
        } else {
            btnStart.setVisibility(View.INVISIBLE);
            btnStop.setVisibility(View.VISIBLE);
        }

    }

    /**
     * 用于调整进度条最大值
     *
     * @param max
     */
    public void setSeekBarMax(int max) {
        seekBar.setMax(max);
    }
}
